//
//  OneDetailViewController.m
//  FruitsWholesaler
//
//  Created by sgone-2 on 15/11/26.
//  Copyright © 2015年 sgone. All rights reserved.
//

#import "OneDetailViewController.h"
#import "AFNetworking.h"
#import "UIImageView+WebCache.h"
//#import "MBProgressHUD+MJ.h"
@interface OneDetailViewController ()
@property (weak, nonatomic) IBOutlet UITableView *tableView;
//获取网络信息的数组
@property(nonatomic,strong) NSMutableDictionary *list;

//获得全部的商品数据
@property(nonatomic,strong) NSMutableArray *list2;
//网路请求
@property(nonatomic,strong)AFHTTPSessionManager *manager;


@end

@implementation OneDetailViewController


//隐藏tabbar
-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.hidesBottomBarWhenPushed = YES;
        
    }
    return self;
}


//afnetworking网络请求数据
- (AFHTTPSessionManager *)manager
{
    if (_manager == nil) {
        _manager  = [AFHTTPSessionManager manager];
    }
    return _manager;
    
    
}



- (void)viewDidLoad {
    [super viewDidLoad];

    self.backBtn.hidden = NO;
    self.title = @"订单详情";
    self.list = [NSMutableDictionary dictionaryWithCapacity:0];
    self.list2 = [NSMutableArray arrayWithCapacity:0];

    self.tableView.backgroundColor = BACKGROUND2;

    [self sendAPI];

}



-(void)sendAPI
{

    NSString *urlStr = [NSString stringWithFormat:@"%@order_info",SERVER_ADDRESS2];
        NSDictionary *params = @{@"wTel" : self.dataHandler.phone , @"uToken" : self.dataHandler.uToken , @"order_id" : self.oID};
    [self.manager POST:urlStr parameters:params success:^(NSURLSessionDataTask *task, id responseObject) {
        
        
//       NSLog(@"99999990 %@",responseObject[@"data"]);
         if ([responseObject[@"error_flag"] intValue] == 0)
         {
             if (![responseObject[@"data"] isKindOfClass:[NSDictionary class]]) {
                 
              
                 
                 return ;
             }
             else
             {
                  NSDictionary *dict = responseObject[@"data"][@"info"];
                 
                 if (dict.count != 0 )
                 {
                     self.list = [NSMutableDictionary dictionaryWithDictionary:dict];
                     self.list2 = [NSMutableArray arrayWithArray:[self.list objectForKey:@"product_list"]];
                 
                 }
                 
             }
             
             
             [self.tableView reloadData];

         
         
         
         
         }
        
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
       
        UIAlertView *alter = [[UIAlertView alloc]initWithTitle:@"提示" message:@"请检查网络!" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
        [alter show];
        
    }];



}





-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{


    return 1;

}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    return self.list2.count;

}


-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{


    return 440+10;

}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{

    return 44*3+64+10+60;

}



-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{

    UIView *view2 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 10, 10)];
    view2.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:view2];
    
    
    
    UIView *view5 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ViewWidth, 44*3 +10)];
    view5.backgroundColor = BACKGROUND2;
    [view2 addSubview:view5];

    UIView *view3 = [[UIView alloc]initWithFrame:CGRectMake(8, 8, ViewWidth-16, 44*3 - 8)];
    view3.backgroundColor = [UIColor whiteColor];
    [view2 addSubview:view3];
    
    
    
    UIView *view4 = [[UIView alloc]initWithFrame:CGRectMake(8, 8, ViewWidth-16, 44)];
    view4.backgroundColor = TiaoColor ;
    [view2 addSubview:view4];
    
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(14,12 , 36, 36)];
    
    NSString *shopUrl = [NSString stringWithFormat:@"%@",[self.list objectForKey:@"shopUrl"]];
    
     NSString *shopImage = [NSString stringWithFormat:@"%@",[self.list objectForKey:@"shopImage"]];
    NSString *imageUrl = [NSString stringWithFormat:@"%@/%@",shopUrl,shopImage];
    NSURL *url = [NSURL URLWithString:imageUrl];
    
    [imageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"backImage"]];
    [view2 addSubview:imageView];

    
    UILabel *lblName = [[UILabel alloc]initWithFrame:CGRectMake(56, 8, 200, 44)];
    lblName.text = [NSString stringWithFormat:@"%@",[self.list objectForKey:@"shopName"]];
    lblName.textColor = [UIColor whiteColor];
    lblName.font = [UIFont systemFontOfSize:16];
    [view2 addSubview:lblName];
    
    
    
    
    
    UIImageView *imagePhone = [[UIImageView alloc]initWithFrame:CGRectMake(20, 52+15, 50 , 50)];
    imagePhone.image = [UIImage imageNamed:@"phone"];
    [view2 addSubview:imagePhone];
    UILabel *lblPhone =[[UILabel alloc]initWithFrame:CGRectMake(73, 52+15+10, 100, 30)];
    lblPhone.font = [UIFont systemFontOfSize:14];
    lblPhone.text = @"联系商家";
    [view2 addSubview:lblPhone];
    
    
    
    
    UIImageView *imageHelp = [[UIImageView alloc]initWithFrame:CGRectMake(ViewWidth -150, 52+15, 50 , 50)];
    imageHelp.image = [UIImage imageNamed:@"help"];
    [view2 addSubview:imageHelp];
    UILabel *lblHelp =[[UILabel alloc]initWithFrame:CGRectMake(ViewWidth -98, 52+15+10, 90, 30)];
    lblHelp.font = [UIFont systemFontOfSize:14];
    lblHelp.text = @"联系经销商";
    [view2 addSubview:lblHelp];
    
    
    
    
    
    
    UIButton *btnPhone = [[UIButton alloc]initWithFrame:CGRectMake(8, 52+15, 136, 50)];
//    btnPhone.backgroundColor = [UIColor greenColor];
    [view2 addSubview:btnPhone];
    [btnPhone addTarget:self action:@selector(phone:) forControlEvents:UIControlEventTouchUpInside];
    
    
    UIButton *btnHelp = [[UIButton alloc]initWithFrame:CGRectMake(ViewWidth *0.57, 52+15, 136, 50)];
//    btnHelp.backgroundColor = [UIColor greenColor];
    [view2 addSubview:btnHelp];
    [btnHelp addTarget:self action:@selector(help:) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    
    //订单详情
    UILabel *lbl1 = [[UILabel alloc]initWithFrame:CGRectMake(8, 44*3+10 , 100, 44)];
    lbl1.text = @"订单编号";
    lbl1.textColor = [UIColor grayColor];
    [view2 addSubview:lbl1];
    lbl1.font = [UIFont systemFontOfSize:15];
    
    
    UIView *view10 = [[UIView alloc]initWithFrame:CGRectMake(0, 44*3+10+43, ViewWidth, 0.3)];
    view10.backgroundColor = [UIColor grayColor];
    [view2 addSubview:view10];
    
    UILabel *lblnumber = [[UILabel alloc]initWithFrame:CGRectMake(90, 44*3+10, ViewWidth *0.6, 44)];
    lblnumber.text = [NSString stringWithFormat:@"%@",[self.list  objectForKey:@"order_id"]];
    [view2 addSubview:lblnumber];
    lblnumber.font = [UIFont systemFontOfSize:15];
    
    
    
    
    UILabel *lbl2 = [[UILabel alloc]initWithFrame:CGRectMake(8, 44*4+10 , 100, 44)];
    lbl2.text = @"联系电话";
    lbl2.textColor = [UIColor grayColor];
    [view2 addSubview:lbl2];
    lbl2.font = [UIFont systemFontOfSize:15];
    
    
    UIView *view11 = [[UIView alloc]initWithFrame:CGRectMake(0, 44*4+10+43, ViewWidth, 0.3)];
    view11.backgroundColor = [UIColor grayColor];
    [view2 addSubview:view11];
    
    UILabel *lblPhone2 = [[UILabel alloc]initWithFrame:CGRectMake(90, 44*4+10, ViewWidth *0.6, 44)];
    lblPhone2.text = [NSString stringWithFormat:@"%@",[self.list  objectForKey:@"sTel"]];
    [view2 addSubview:lblPhone2];
    lblPhone2.font = [UIFont systemFontOfSize:15];
    
    
    
    
    
    UILabel *lbl3 = [[UILabel alloc]initWithFrame:CGRectMake(8, 44*5+10 , 100, 44)];
    lbl3.text = @"送货距离";
    lbl3.textColor = [UIColor grayColor];
    [view2 addSubview:lbl3];
    lbl3.font = [UIFont systemFontOfSize:15];
    
    
    UIView *view12 = [[UIView alloc]initWithFrame:CGRectMake(0, 44*5+10+43, ViewWidth, 0.3)];
    view12.backgroundColor = [UIColor grayColor];
    [view2 addSubview:view12];
    
    UILabel *lblDistance = [[UILabel alloc]initWithFrame:CGRectMake(90, 44*5+10, ViewWidth *0.6, 44)];
    lblDistance.text = [NSString stringWithFormat:@"%@米 (%@)",[self.list  objectForKey:@"distance"],[self.list objectForKey:@"type"]];
    [view2 addSubview:lblDistance];
    lblDistance.font = [UIFont systemFontOfSize:15];
    
    
    
    
    
    UILabel *lbl4 = [[UILabel alloc]initWithFrame:CGRectMake(8, 44*6+10 , 100, 44)];
    lbl4.text = @"下单时间";
    lbl4.textColor = [UIColor grayColor];
    [view2 addSubview:lbl4];
    lbl4.font = [UIFont systemFontOfSize:15];
    
    
    UIView *view13 = [[UIView alloc]initWithFrame:CGRectMake(0, 44*6+10+43, ViewWidth, 0.3)];
    view13.backgroundColor = [UIColor grayColor];
    [view2 addSubview:view13];
    
    UILabel *lblTime1 = [[UILabel alloc]initWithFrame:CGRectMake(90, 44*6+10, ViewWidth *0.6, 44)];
    
    
    
    int ordertime = [self.list[@"order_time"] intValue];
    NSDate *orderdate = [[NSDate alloc]initWithTimeIntervalSince1970:ordertime];
    NSDateFormatter *formatter2 = [[NSDateFormatter alloc]init];
    [formatter2 setDateFormat:@"yyy-MM-dd  HH:mm"];
 
    lblTime1.text =  [formatter2 stringFromDate:orderdate];
    [view2 addSubview:lblTime1];
    lblTime1.font = [UIFont systemFontOfSize:15];


    
    
    
    
    
    UILabel *lbl5 = [[UILabel alloc]initWithFrame:CGRectMake(8, 44*7+10 , 100, 44)];
    lbl5.text = @"送货时间";
    lbl5.textColor = [UIColor grayColor];
    [view2 addSubview:lbl5];
    lbl5.font = [UIFont systemFontOfSize:15];
    
    
    UIView *view14 = [[UIView alloc]initWithFrame:CGRectMake(0, 44*7+10+43, ViewWidth, 0.3)];
    view14.backgroundColor = [UIColor grayColor];
    [view2 addSubview:view14];
    
    UILabel *lblTime2 = [[UILabel alloc]initWithFrame:CGRectMake(90, 44*7+10, ViewWidth *0.6, 44)];
    
    
    
    int ordertime2 = [self.list[@"point_time"] intValue];
    NSDate *orderdate2 = [[NSDate alloc]initWithTimeIntervalSince1970:ordertime2];
    NSDateFormatter *formatter3 = [[NSDateFormatter alloc]init];
    [formatter3 setDateFormat:@"yyy-MM-dd  HH:mm"];
    
    lblTime2.text =  [formatter3 stringFromDate:orderdate2];
    [view2 addSubview:lblTime2];
    lblTime2.font = [UIFont systemFontOfSize:15];



    
    UILabel *lbl6 = [[UILabel alloc]initWithFrame:CGRectMake(8, 44*8+10 , 100, 44)];
    lbl6.text = @"送货地址";
    lbl6.textColor = [UIColor grayColor];
    [view2 addSubview:lbl6];
    lbl6.font = [UIFont systemFontOfSize:15];
    
    
    UIView *view15 = [[UIView alloc]initWithFrame:CGRectMake(0, 44*8+10+43, ViewWidth, 0.3)];
    view15.backgroundColor = [UIColor grayColor];
    [view2 addSubview:view15];
    
    UILabel *lblAddress = [[UILabel alloc]initWithFrame:CGRectMake(90, 44*8+10, ViewWidth *0.65, 44)];
    lblAddress.text = [NSString stringWithFormat:@"%@",[self.list objectForKey:@"address"]];
    [view2 addSubview:lblAddress];
    lblAddress.font = [UIFont systemFontOfSize:14];

    
    
    

    
    //设置送货清单信息提示
    UIView *view20 = [[UIView alloc]initWithFrame:CGRectMake(0, 44*9+10, ViewWidth, 44)];
    view20.backgroundColor = BACKGROUND2;
    [view2 addSubview:view20];
    
    UILabel *lblS = [[UILabel alloc]initWithFrame:CGRectMake(8, 0, 100, 44)];
    lblS.text = @"送货清单";
    [view20 addSubview:lblS];
    lblS.textColor = [UIColor grayColor];
    lblS.font = [UIFont systemFontOfSize:15];
    
    
    UILabel *lblD = [[UILabel alloc]initWithFrame:CGRectMake(ViewWidth *0.52, 0, 50, 44)];
    lblD.text = @"单价";
    [view20 addSubview:lblD];
    lblD.textColor = [UIColor grayColor];
    lblD.font = [UIFont systemFontOfSize:15];
    
    
    
    UILabel *lblSL = [[UILabel alloc]initWithFrame:CGRectMake(ViewWidth*0.52+50 , 0, 50, 44)];
    lblSL.text = @"数量";
    [view20 addSubview:lblSL];
    lblSL.textColor = [UIColor grayColor];
    lblSL.font = [UIFont systemFontOfSize:15];
    
    
    
    UILabel *lblDJ = [[UILabel alloc]initWithFrame:CGRectMake(ViewWidth*0.52+50+50 , 0, 50, 44)];
    lblDJ.text = @"价格";
    lblDJ.textAlignment = NSTextAlignmentCenter;
    [view20 addSubview:lblDJ];
    lblDJ.textColor = [UIColor grayColor];
    lblDJ.font = [UIFont systemFontOfSize:15];
    
    
    
    
    
    
    
    

    return view2;

}

#pragma 商家电话
-(void)phone:(id)sender
{

    UIAlertView *alter = [[UIAlertView alloc]initWithTitle:@"温馨提示" message:@"是否拨打商家电话?" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    [alter show];
    alter.tag = 111;
    
    

}



- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    
    if (alertView.tag==111) {
        
        NSString *button=[alertView buttonTitleAtIndex:buttonIndex];
        
        if ([button isEqualToString:@"确定"]) {
            //拨打电话
            NSString *phoneNumber = [@"tel://" stringByAppendingString:[NSString stringWithFormat:@"%@",[self.list objectForKey:@"sTel"]]];
            
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumber]];
            
            
        }
        else if([button isEqualToString:@"取消"])
        {
            
        }
    }
    else if (alertView.tag == 112)
    {
        NSString *button=[alertView buttonTitleAtIndex:buttonIndex];
        
        if ([button isEqualToString:@"确定"])
        {

            //拨打电话
            NSString *phoneNumber = [@"tel://" stringByAppendingString:[NSString stringWithFormat:@"%@",[self.list objectForKey:@"dealer_name"]]];
            
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumber]];
        
        }
    
    
    
    
    
    }
}






-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{

    
    UIView *view12 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 10, 10)];
    view12.backgroundColor = BACKGROUND2;
    [self.view addSubview:view12];
    
    
    
    UIView*view13 = [[UIView alloc]initWithFrame:CGRectMake(0, 10, ViewWidth, 44*3-10+10)];
    view13.backgroundColor= [UIColor whiteColor];
    [view12 addSubview:view13];
    
    UIView *view21 = [[UIView alloc]initWithFrame:CGRectMake(0, 53, ViewWidth, 0.3)];
    view21.backgroundColor = [UIColor grayColor];
    [view12 addSubview:view21];
    
    UIView *view22 = [[UIView alloc]initWithFrame:CGRectMake(0, 53+44, ViewWidth, 0.3)];
    view22.backgroundColor = [UIColor grayColor];
    [view12 addSubview:view22];
    
    
    
    UILabel *lbl1 = [[UILabel alloc]initWithFrame:CGRectMake(8, 10, 100, 44)];
    lbl1.text = @"商品金额";
    lbl1.textColor = [UIColor grayColor];
    lbl1.font = [UIFont systemFontOfSize:15];
    [view12 addSubview:lbl1];
    
    UILabel *lblAccount = [[UILabel alloc]initWithFrame:CGRectMake(ViewWidth *0.65, 10, 100, 44)];
    lblAccount.text = [NSString stringWithFormat:@"￥%@",[self.list objectForKey:@"wholesaler_amount"]];
    lblAccount.textColor = BenSe_COLOR;
    lblAccount.font = [UIFont systemFontOfSize:14];
    [view12 addSubview:lblAccount];
    lblAccount.textAlignment = NSTextAlignmentRight;
    
    
    
    
    UILabel *lbl2 = [[UILabel alloc]initWithFrame:CGRectMake(8, 10+44, 100, 44)];
    lbl2.text = @"产生运费";
    lbl2.textColor = [UIColor grayColor];
    lbl2.font = [UIFont systemFontOfSize:15];
    [view12 addSubview:lbl2];
    
    
    UILabel *lblTrans = [[UILabel alloc]initWithFrame:CGRectMake(ViewWidth *0.65, 10+44, 100, 44)];
    lblTrans.text = [NSString stringWithFormat:@"￥%@",[self.list objectForKey:@"trans_amount"]];
    lblTrans.textColor = BenSe_COLOR;
    lblTrans.font = [UIFont systemFontOfSize:14];
    [view12 addSubview:lblTrans];
    lblTrans.textAlignment = NSTextAlignmentRight;
    
    
    
    
    UILabel *lbl3 = [[UILabel alloc]initWithFrame:CGRectMake(8, 10+44*2, 100, 44)];
    lbl3.text = @"所属经销商";
    lbl3.textColor = [UIColor grayColor];
    lbl3.font = [UIFont systemFontOfSize:15];
    [view12 addSubview:lbl3];
    
    
    
    UILabel *lblJX = [[UILabel alloc]initWithFrame:CGRectMake(ViewWidth -150, 10+44*2, 140, 44)];
    lblJX.text = [NSString stringWithFormat:@"%@",[self.list objectForKey:@"dealer_name"]];
//    lblJX.textColor = BenSe_COLOR;
    lblJX.font = [UIFont systemFontOfSize:15];
    [view12 addSubview:lblJX];
    lblJX.textAlignment = NSTextAlignmentRight;

    
    
   
    //添加确认发货
    UIButton *queren = [[UIButton alloc]initWithFrame:CGRectMake(0,  44*3+64+10+5, ViewWidth, 55)];
    queren.backgroundColor = BenSe_COLOR;
    [queren setTitle:@"确 认 发 货" forState:UIControlStateNormal];
    queren.titleLabel.font = [UIFont systemFontOfSize:19];
    [queren addTarget:self action:@selector(queren:) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    [view12 addSubview:queren];


    return view12;

}



#pragma mark 确认发货
-(void)queren:(id)sender
{

    
//    NSLog(@"89809809");
    NSString *urlStr = [NSString stringWithFormat:@"%@order_send",SERVER_ADDRESS2];
    NSDictionary *params = @{@"wTel" : self.dataHandler.phone , @"uToken" : self.dataHandler.uToken , @"order_id" : self.oID };
    
    [self.manager POST:urlStr parameters:params success:^(NSURLSessionDataTask *task, id responseObject) {
//        NSLog(@"送货成功  %@   %@",responseObject,responseObject[@"result_msg"]);

        if ([responseObject[@"error_flag"]intValue] == 0) {
            
            
            
            
            
            //通知前一面进行刷新
            [[NSNotificationCenter defaultCenter] postNotificationName:@"fahuo" object:nil userInfo:nil ];
            
            [self.navigationController popViewControllerAnimated:YES];
            
        }
        else
        {
        
            NSString *str = [NSString stringWithFormat:@"%@",responseObject[@"result_msg"]];
            UIAlertView  *alter = [[UIAlertView alloc]initWithTitle:@"提示" message:str delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
            [alter show];
        
        
        
        }
        
        
    }
    failure:^(NSURLSessionDataTask *task, NSError *error) {
        
        UIAlertView *alter = [[UIAlertView alloc]initWithTitle:@"提示" message:@"请检查网络!" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
        [alter show];
        
        
    }];



}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{


    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        
    }

    //订单详情
    UILabel *lbl = [[UILabel alloc]initWithFrame:CGRectMake(8, 0, 200, 44)];
    lbl.text = [NSString stringWithFormat:@"%@",[self.list2 [indexPath.row] objectForKey:@"product_name"]];
    lbl.font = [UIFont systemFontOfSize:15];
    [cell addSubview:lbl];
    
    
    
    UILabel *lbl2 = [[UILabel alloc]initWithFrame:CGRectMake(ViewWidth*0.52-8, 0, 60, 44)];
    lbl2.text = [NSString stringWithFormat:@"￥%@",[self.list2 [indexPath.row] objectForKey:@"unit_price"]];
    lbl2.font = [UIFont systemFontOfSize:14];
    [cell addSubview:lbl2];
    
    
    
    UILabel *lbl3 = [[UILabel alloc]initWithFrame:CGRectMake(ViewWidth *0.52+50, 0, 60, 44)];
    lbl3.text = [NSString stringWithFormat:@"X%@",[self.list2 [indexPath.row] objectForKey:@"product_num"]];
    lbl3.font = [UIFont systemFontOfSize:14];
    [cell addSubview:lbl3];
    
    
    UILabel *lbl4 = [[UILabel alloc]initWithFrame:CGRectMake(ViewWidth *0.52+100-3, 0, 60, 44)];
    lbl4.text = [NSString stringWithFormat:@"￥%@",[self.list2 [indexPath.row] objectForKey:@"total_price"]];
    lbl4.font = [UIFont systemFontOfSize:14];
    [cell addSubview:lbl4];
    
    
    
    
   
    
    
  
    
    
    
    
    
    
    return cell;



}


@end
